
import os
from google.appengine.ext.webapp import template

import cgi
import logging

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Nugget(db.Model):
    author = db.UserProperty()
    comment = db.StringProperty(multiline=True)
    listKeyId = db.IntegerProperty()
    date = db.DateTimeProperty(auto_now_add=True)

class NuggetList(db.Model):
    author = db.UserProperty()
    title = db.StringProperty()    
    date = db.DateTimeProperty(auto_now_add=True)

class Nuggets(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()        
        
        list_query = NuggetList.all()
        list_query.filter('author = ', user)                
        lists = list_query.fetch(10)


        if user:
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'logout'
            name = user.nickname()
            signedIn = True
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'login'
            name = None
            signedIn = False

        template_values = {
            'lists': lists,
            'url': url,
            'url_linktext': url_linktext,
            'name': name,
            'signedIn': signedIn
        }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class CreateNugget(webapp.RequestHandler):
    def post(self):
        listKeyFromRequest = int(self.request.get('listKey'))
        zeKey = db.Key.from_path('NuggetList', listKeyFromRequest)
        nuggetList = db.get(zeKey)        
        nugget = Nugget()
        if users.get_current_user():
            nugget.author = users.get_current_user()
        nugget.comment = self.request.get('comment')
        zeNewKey = nuggetList.key()
        nugget.listKeyId = zeNewKey.id()
        nugget.put()
        
        nugget_query = Nugget.all()
        nugget_query.filter('listKeyId = ', listKeyFromRequest)
        nuggets = nugget_query.fetch(10)
        template_values = {
            'nuggets': nuggets,
            'listKeyId': listKeyFromRequest
        }

        path = os.path.join(os.path.dirname(__file__), 'expandList.html')
        self.response.out.write(template.render(path, template_values))


class CreateNuggetList(webapp.RequestHandler):
    def post(self):
        newListName = self.request.get('newList')
        nuggetList = NuggetList()
        nuggetList.title = newListName
        if users.get_current_user():
            nuggetList.author = users.get_current_user()
        nuggetList.put()
        template_values = {
            'list': nuggetList
        }
        path = os.path.join(os.path.dirname(__file__), 'newList.html')
        self.response.out.write(template.render(path, template_values))

class ExpandList(webapp.RequestHandler):
    def get(self):
        listKey = int(self.request.get('listKey'))
        nugget_query = Nugget.all()
        nugget_query.filter('listKeyId = ', listKey)
        nuggets = nugget_query.fetch(10)
        template_values = {
            'nuggets': nuggets,
            'listKeyId': listKey
        }

        path = os.path.join(os.path.dirname(__file__), 'expandList.html')
        self.response.out.write(template.render(path, template_values))
        

application = webapp.WSGIApplication(
                                     [('/', Nuggets),
                                      ('/create', CreateNugget),
                                      ('/createList', CreateNuggetList),
                                      ('/expandList', ExpandList)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()